package com.hongying.lingtianxiang;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class PosterProduct2 {
    public static void main(String[] args) throws IOException {
        BufferedImage bg1 = ImageIO.read(new File("bg1-2.png"));
        BufferedImage bg2 = ImageIO.read(new File("bg2-2.png"));
        BufferedImage head = ImageIO.read(new File("head-1.png"));
        BufferedImage code = ImageIO.read(new File("qrcode.jpg"));


        Graphics2D g1 = bg2.createGraphics();

        // 绘制头像
        Ellipse2D.Double shape = new Ellipse2D.Double(43, 89, 110, 108);
        g1.setClip(shape);
        g1.drawImage(head, 43, 89, 110, 108, null);
        g1.setClip(null);

        // 绘制头像边框（遮盖剪贴圆留下的毛边）
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g1.setColor(Color.BLACK);
        g1.drawOval(43,89,110,108);

        // 绘制昵称
        Font font = new Font("微软雅黑", Font.PLAIN, 30);
        g1.setFont(font);
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g1.setColor(new Color(102, 102, 102));
        String text = "一天到晚游泳的鱼";
        g1.drawString(text, 181, 144);

        // 绘制邀请规则
        font = new Font("微软雅黑", Font.PLAIN, 28);
        g1.setFont(font);
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g1.setColor(new Color(136, 136, 136));
        text = "邀请规则";
        g1.drawString(text, 42, 296);

        // 绘制邀请规则内容
        font = new Font("微软雅黑", Font.PLAIN, 25);
        g1.setFont(font);
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        text = "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊\r\n有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有有\r\n钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱钱";
        writeTextNewline(g1, text, 42, 350);

        // 绘制长按识别二维码文字
        text = "长按识别二维码";
        g1.drawString(text, 42, 681);

        // 绘制二维码图片
//        g1.drawImage(code, 437, 601, code.getWidth()/2, code.getHeight()/2, null);
        System.out.println(code.getWidth()/3+36);
        System.out.println(code.getHeight()/3+36);
        g1.drawImage(code, 370, 558, code.getWidth()/3+36, code.getHeight()/3+36, null);

        g1.dispose();

        // 绘制图片
        Graphics2D g2 = bg1.createGraphics();
        g2.drawImage(bg2, 76, 225, 600, 766, null);
        g2.dispose();

        ImageIO.write(bg1, "png", new File("C:\\Users\\qwer\\Desktop\\a\\poster.png"));
    }

    // 获取网络图片
    public BufferedImage getUrlImg(String urlImg) {
        BufferedImage img = null;
        try {
            URL url = new URL(urlImg);
            InputStream is = url.openConnection().getInputStream();
            img = ImageIO.read(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return img;
    }

    /**
     * 写换行文本
     * @param g
     * @param text
     * @param x
     * @param y
     */
    private static void writeTextNewline(Graphics g, String text, int x, int y){
        //获取字符高度
        int strHeight = getStringHeight(g);
        String[] strs = text.split("\r\n");
        for (int i = 0; i < strs.length; i++) {
            //获取字符串 字符的总宽度
            int strWidth = getStringLength(g, strs[i]);
            //每一行字符串宽度
            int rowWidth = 520;
            int rowstrnum = getRowStrNum(strs[i].length(), rowWidth, strWidth);
            int rows = getRows(strWidth, rowWidth);
            String temp = "";
            for (int j = 0; j < rows; j++) {
                //获取各行的String
                if (j == rows - 1) {
                    //最后一行
                    temp = strs[i].substring(j * rowstrnum, strs[i].length());
                } else {
                    temp = strs[i].substring(j * rowstrnum, j * rowstrnum + rowstrnum);
                }
                if (j > 0) {
                    //第一行不需要增加字符高度，以后的每一行在换行的时候都需要增加字符高度
                    y = y + strHeight;
                }
                g.drawString(temp, x, y);
            }
            y = y + strHeight;
            System.out.println("y"+y);
        }
    }

    //java画图时切割字符串，font就一行显示多少字=控件宽度/字体大小-首尾空格（width/fontSize - 2）

    //字符串总宽度
    private static int getStringLength(Graphics g, String str) {
        char[] strcha = str.toCharArray();
        int strWidth = g.getFontMetrics().charsWidth(strcha, 0, str.length());
        System.out.println("字符总宽度:" + strWidth);
        return strWidth;
    }

    //每一行字符的个数
    private static int getRowStrNum(int strnum, int rowWidth, int strWidth) {
        int rowstrnum = 0;
        rowstrnum = (rowWidth * strnum) / strWidth;
        System.out.println("每行的字符数:" + rowstrnum);
        return rowstrnum;
    }

    //字符行数
    private static int getRows(int strWidth, int rowWidth) {
        int rows = 0;
        if (strWidth % rowWidth > 0) {
            rows = strWidth / rowWidth + 1;
        } else {
            rows = strWidth / rowWidth;
        }
        System.out.println("行数:" + rows);
        return rows;
    }

    //字符高度
    private static int getStringHeight(Graphics g) {
        int height = g.getFontMetrics().getHeight();
        System.out.println("字符高度:" + height);
        return height;
    }

    private static void drawStringWithFontStyleLineFeed(Graphics g, String strContent, int loc_X, int loc_Y) {
        //获取字符串 字符的总宽度
        int strWidth = getStringLength(g, strContent);
        //每一行字符串宽度
        int rowWidth = 520;
        System.out.println("每行字符宽度:" + rowWidth);
        //获取字符高度
        int strHeight = getStringHeight(g);
        //字符串总个数
        System.out.println("字符串总个数:" + strContent.length());
        if (strWidth > rowWidth) {
            int rowstrnum = getRowStrNum(strContent.length(), rowWidth, strWidth);
            int rows = getRows(strWidth, rowWidth);
            String temp = "";
            for (int i = 0; i < rows; i++) {
                //获取各行的String
                if (i == rows - 1) {
                    //最后一行
                    temp = strContent.substring(i * rowstrnum, strContent.length());
                } else {
                    temp = strContent.substring(i * rowstrnum, i * rowstrnum + rowstrnum);
                }
                if (i > 0) {
                    //第一行不需要增加字符高度，以后的每一行在换行的时候都需要增加字符高度
                    loc_Y = loc_Y + strHeight;
                }
                g.drawString(temp, loc_X, loc_Y);
            }
        } else {
            //直接绘制
            g.drawString(strContent, loc_X, loc_Y);
        }

    }
}
